﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;

public partial class map_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private SessionHelper m_SessionHelper;

    private int m_ParentNodeId = 40;

    private int m_PostId = 41;
    private int m_NodeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_SessionHelper = new SessionHelper();
        m_PostId = QueryStringHelper.GetInteger("post", 0);

        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        if (!IsPostBack)
        {
            if (m_PostId == 0)
            {
                Response.Redirect("discuss.aspx");
            }

            PostVO postVO = m_PostService.GetPostByIdNoLazy(m_PostId);
            if (postVO != null)
            {
                m_NodeId = postVO.Node.NodeId;
                ltlName.Text = postVO.Node.Name;
                ltlTitle.Text = postVO.Title;
            }
        }
    }        

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        PostVO postVO = new PostVO();
        UIHelper.FillVO(PanelUI, postVO);
        postVO.ParentPost = m_PostService.GetPostById(m_PostId);
        postVO.CreatedDate = DateTime.Now;
        postVO.CreatedBy = m_SessionHelper.Member.LoginId;
        postVO.Flag = 1;
        postVO = m_PostService.CreatePost(postVO);

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("回覆成功。", string.Format("discuss_in.aspx?post={0}", m_PostId)), false);
    }
}